import { createStore } from 'vuex'
import { Token } from '../utils/request'
export default createStore({
  state: {
    user: {
      nikeName: localStorage.getItem('nikeName') || ''// 确保初始化为空字符串或其他默认值
    }
  },
  getters: {
    getUserNickName: (state) => state.user.nikeName
  },
  mutations: {
    setNikeName(state, name) {
      state.user.nikeName = name
      console.log('setNikeName', name)
    },
    setUserInfo(state, userInfo) {
      state.user = userInfo
      localStorage.setItem('nikeName', userInfo.nikeName) // 保存到localStorage
    }
  },
  actions: {
    async fetchUserInfo({ commit }) {
      // 模拟API请求
      const response:any= await Token('/api/info', 'get', {})
      commit('setUserInfo', response.data)
    }
  },
  modules: {
  }
})
